package com.plian.system.service.mt.mtgroup;

import com.plian.Tools.TokenUtil;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.dao.mt.mtgroup.MTGroupDao;
import com.plian.system.dao.sys.file.FileDao;
import com.plian.system.dao.sys.orgzation.OrgzationDao;
import com.plian.system.dao.sys.per.UserOrgDao;
import com.plian.system.dao.sys.user.UserDao;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.page.Page;
import com.plian.system.service.sys.file.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class MTGroupService {
    @Autowired
    MTGroupDao mTGroupDao;
    @Autowired
    UserDao userDao;
    @Autowired
    UserOrgDao userOrgDao;
    @Autowired
    OrgzationDao orgzationDao;
    @Autowired
    FileService fileService;
    @Autowired
    FileDao fileDao;

    /**
     * 保存
     *
     * @param pageData
     * @return
     */
    public ObjResult save(PageData pageData) {
        try {
            pageData.put("id", UuidUtil.get32UUID());
            mTGroupDao.save(pageData);
            return new ObjResult(CommonCode.SUCCESS);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult(CommonCode.FAIL));
        }
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    public ObjResult delete(List<String> ids) {
        try {
            mTGroupDao.delete(ids);
            return new ObjResult(CommonCode.SUCCESS);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult(CommonCode.FAIL));
        }
    }

    /**
     * 修改
     *
     * @param pageData
     * @return
     */
    public ObjResult edit(PageData pageData) {
        try {
            mTGroupDao.edit(pageData);
            return new ObjResult(CommonCode.SUCCESS);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult(CommonCode.FAIL));
        }
    }

    /**
     * 单据详情
     *
     * @param pageData
     * @return
     */
    public ObjResult findbyid(PageData pageData) {
        try {
            ObjResult objResult;
            PageData p = mTGroupDao.findById(pageData.getString("id"));
            objResult = new ObjResult(CommonCode.SUCCESS);
            if(null != p.getString("userId") && !"".equals(p.getString("userId"))){
                String[] userId = p.getString("userId").split(",");
                List<PageData> l = new ArrayList<>();
                for (String s :
                        userId) {
                    PageData pd =  userDao.findbyid(s);
                    l.add(pd);
                }
                p.put("userInfo",l);
            }
            objResult.setPageData(p);
            return objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult(CommonCode.ERROR));
        }
    }

    /**
     * 查询列表
     *
     * @param pageData
     * @return
     */
    public ListResult list(PageData pageData) {
        ListResult listResult;
        try {
            if (null != pageData.getString("orgId") && !"".equals(pageData.getString("orgId"))) {
                Page page = new Page(true, pageData);
                listResult = new ListResult(CommonCode.SUCCESS);
                List<PageData> list = mTGroupDao.list(page);
                if (list.size() > 0) {
                    for (PageData pd : list) {
                        String userId = pd.getString("userId");
                        if (null != userId && !"".equals(userId)) {
                            String[] UserId = userId.split(",");
                            List<String> id = new ArrayList<String>(Arrays.asList(UserId));
                            pd.put("userNameList", userDao.findListById(id));
                        }
                    }
                }
                listResult.setList(list);
            } else {
                listResult = new ListResult(CommonCode.SUCCESS);
                listResult.setList(null);
            }
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            listResult = new ListResult(CommonCode.ERROR);
        }
        return listResult;
    }

    public ListResult unit() {
        try {
            PageData pd = new PageData();
            PageData pdUser = userDao.findbyid(TokenUtil.getTokenUserId());
            List<PageData> list = new ArrayList<>();
            //判断是否为企业账号
            if ("3".equals(pdUser.getString("type"))) {
                List<PageData> arrlist = userOrgDao.queryUserOrg(pdUser.getString("id"));
                PageData pageData = new PageData();
                for (PageData p :
                        arrlist) {
                    pageData = new PageData();
                    pageData.put("orgId", p.getString("orgId"));
                    pageData.put("orgName", p.getString("orgName"));
                    list.add(pageData);
                }
            } else if ("1".equals(pdUser.getString("type"))) {
                list = orgzationDao.list(pd);
            }
            ListResult listResult = new ListResult(CommonCode.SUCCESS);
            listResult.setList(list);
            return listResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult(CommonCode.FAIL));
        }
    }
}
